package com.jwell.panrank.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panrank.R
import com.jwell.panrank.contract.ForgetPasswordContract
import com.jwell.panrank.presenter.ForgetPasswordPresenter
import com.jwell.panrank.tools.CheckPhone
import kotlinx.android.synthetic.main.activity_forget_password.*

/**
 * 忘记密码
 */
class ForgetPasswordActivity : MVPBaseActivity<ForgetPasswordPresenter>(), ForgetPasswordContract.View, View.OnClickListener {


    override fun createPresenter(): ForgetPasswordPresenter? {
        return ForgetPasswordPresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_forget_password)
        initToolBar("忘记密码", true, 0x00000000)
        setListener()
    }

    private fun setListener() {
        btn_next_step.setOnClickListener(this)
        btn_old_code.setOnClickListener(this)
        btn_sure.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_next_step -> {
                if (edit_account.text.isNullOrEmpty() || edit_phoneCode.text.isNullOrEmpty()) {
                    return
                }
                ll_verify.visibility = View.GONE
                ll_sure.visibility = View.VISIBLE
            }
            R.id.btn_old_code -> {
                if (!CheckPhone.isPhone(edit_account.text.toString())) {
                    toast("请输入正确的手机号码")
                    return
                }
                showProgress("", true)
                //userType:1供应商，2后台用户
                presenter?.regSendMsg(edit_account.text.toString(), 2)
            }
            R.id.btn_sure -> {
                if (edit_newPassword.text.isNullOrEmpty()) {
                    toast("请输入新密码")
                    return
                }
                if (edit_surePassword.text.isNullOrEmpty()) {
                    toast("请确认新密码")
                    return
                }
                if (edit_surePassword.text.toString() != edit_newPassword.text.toString()) {
                    toast("两次密码不一致")
                    return
                }
                showProgress("", true)
                presenter?.userRegPwd(edit_account.text.toString(), edit_phoneCode.text.toString(), edit_newPassword.text.toString(), 2)
            }
        }
    }

    override fun getCodeSucceed() {
        btn_old_code.startDownTime()
    }

    override fun resetFail() {
        ll_verify.visibility = View.VISIBLE
        ll_sure.visibility = View.GONE
    }

    override fun resetSucceed(newPassword: String) {
        toast("修改成功!")
        var intent = Intent()
        intent.putExtra("pwd", newPassword)
        setResult(Activity.RESULT_OK, intent)
        returnActivity()
    }
}
